package leetcode;

import leetcode.N094BinaryTreeInorderTraversal.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉搜索树中，节点的最小距离。
 * @author yinlei
 * @date 2018/7/16
 * @since 1.0.0
 */
public class N530MinimumAbsoluteDifferenceInBST {
    public int getMinimumDifference(TreeNode root) {

        List<TreeNode> list = new ArrayList<>();

        inOrder(root, list);

        int min = Integer.MAX_VALUE;

        for (int i = 1; i < list.size(); i++) {
            int pm = list.get(i).val - list.get(i - 1).val;
            if (pm < min) {
                min = pm;
            }
        }
        return min;
    }

    private void inOrder(TreeNode root, List<TreeNode> list) {
        if (root == null) return;

        if (root.left != null) {
            inOrder(root.left, list);
        }
        list.add(root);
        if (root.right != null) {
            inOrder(root.right, list);
        }
    }
}
